
$(function() {

	$("input[id^='customer_s']:visible").autocomplete(Test.customer);
	
	Tip.emp('业务员');
});

var Visit = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_add")[0];
	
				var body = { };
				body.emp = form.emp.value;
				body.customer = form.customer.value;
				body.customer_x = Common.customer_x(form);
	
				body.name = form.name.value;
				body.content = form.content.value;
				
				body.t1 = form.date1.value;
				body.t2 = form.date2.value;
				body.day = form.day.value;
	
				$(this).dialog("close");
	
				var url = 'customer.php?do=Visit.addPost';
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);
				
		var c1 = 'customer.php?do=Visit.edit&id=' + id;
		var c2 = 'customer.php?do=Visit.view&id=' + id;

		var a1 = ['编辑信息', 'url', 'Visit.dialog_edit_', '600x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '600x400'];

		var s = c1 + ", " + c2;

		return this.popup_(e, [a1, a2], s);
	},
	
	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var d2 = 'customer.php?do=Visit.copy&id=' + id;
		var b2 = ['复制信息', 'url', 'Visit.dialog_copy_', '650x420'];
		
		var ss = 'customer.php?do=Visit.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Visit.dialog_ss', '400x200'];
		
		s += ", " + d2 + ", " + ss;
		a.push(b2);
		a.push(bb);
			
		return popup(e, a, s);
	},
	
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('customer.php?do=Visit.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},
	
					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
	
						var form = $("#form_edit")[0];
	
						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;
	
						$(this).dialog("close");
	
						var url = 'customer.php?do=Visit.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
	
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
	
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('customer.php?do=Visit.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						body.emp = form.emp.value;
						body.customer = form.customer.value;
						body.customer_x = Common.customer_x(form);
			
						body.name = form.name.value;
						body.content = form.content.value;
						
						body.t1 = form.date1.value;
						body.t2 = form.date2.value;
						body.day = form.day.value;

						$(this).dialog("close");

						var url = 'customer.php?do=Visit.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = { };
				body.id = form.id.value;
				body.emp = form.emp.value;
				body.customer = form.customer.value;
				body.customer_x = Common.customer_x(form);
	
				body.name = form.name.value;
				body.content = form.content.value;
				
				body.t1 = form.date1.value;
				body.t2 = form.date2.value;
				body.day = form.day.value;

				$(this).dialog("close");

				var url = 'customer.php?do=Visit.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	}
};
